package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.operation.mscyb.service.JqbzSettingsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-05-20
 */
@Service
public class JqbzSettingsServiceImpl extends ServiceImpl<JqbzSettingsMapper, JqbzSettings> implements JqbzSettingsService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
    @Override
    public JqbzSettings selectJqbzSettingsById(String id) throws AdscmSystemException {
        JqbzSettings jqbzSettings = super.selectById(id);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings,null)){
            throw new AdscmSystemException("未查找到对应数据");
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings.getCreator(),null)){
            JyUser jyUser = jyUserMapper.selectById(jqbzSettings.getCreator());
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                jqbzSettings.setCreatorName(jyUser.getName());
            }else{
                jqbzSettings.setCreatorName(jqbzSettings.getCreator());
            }
        }

        return jqbzSettings;
    }

    @Override
    public Page<JqbzSettings> selectAll(Page<JqbzSettings> page, QueryJqbzSettings queryJqbzSettings) throws AdscmSystemException {
        Wrapper<JqbzSettings>  wrapper =new EntityWrapper();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getName(),null)){
            wrapper.like("name",queryJqbzSettings.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getCode(),null)){
            wrapper.like("code",queryJqbzSettings.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getIsEnable(),null)){
            wrapper.eq("isEnable",queryJqbzSettings.getIsEnable());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqbzSettings.getType(),null)){
            wrapper.eq("type",queryJqbzSettings.getType());
        }
        wrapper.orderBy("rank",true);
        page = super.selectPage(page, wrapper);
        List<JqbzSettings> jqbzSettings = page.getRecords();
        for (JqbzSettings jqbzSetting : jqbzSettings) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSetting.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqbzSetting.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    jqbzSetting.setCreatorName(jyUser.getName());
                }else {
                    jqbzSetting.setCreatorName(jqbzSetting.getCreator());
                }
            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(jqbzSetting.getPerformer(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqbzSetting.getPerformer());
                jqbzSetting.setPerformerName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSetting.getType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqbzSetting.getType());
                jqbzSetting.setTypeName(jyDrawingType.getType());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSetting.getUseTime(),null)){

                jqbzSetting.setUseTime(jqbzSetting.getUseTime());
            }
        }
        page.setRecords(jqbzSettings);
        page.setTotal(super.selectCount(wrapper));

        return page;
    }

    @Override
    public List<JyDrawingType> selectJyDrawType() {
        List<JyDrawingType> jyDrawingTypes = jyDrawingTypeMapper.selectList(null);
        return jyDrawingTypes;
    }

    @Override
    public List<JqbzSettings> selectAllList() {

        List<JqbzSettings> jqbzSettings = super.selectList(null);
        for (JqbzSettings jqbzSetting : jqbzSettings) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSetting.getType(),null)){
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(jqbzSetting.getType());
                jqbzSetting.setTypeName(jyDrawingType.getType());
            }
        }
        return jqbzSettings;
    }
}
